import { WindowMessenger, connect } from 'penpal';
import { DriverOperationDto } from './dto/driverOperationDto';
import { DriverDto } from './dto/driverDto';

export class Proxy { 

    // 连接对象
    private _cnnection: any;  

    // 初始化
    public init: () => Promise<void> = async () => { 

        const messenger = new WindowMessenger({
            remoteWindow: window.parent,
            allowedOrigins: ['*'],
        });
    
        const connection = connect({
            messenger,
            methods: {
                // setInfo(info: any) {
                //     that._info = info; 
                // },
            },
            timeout:1000
        });

        this._cnnection = await connection.promise as any;
    } 

    // 信息
    public info: () => Promise<string> = async () =>{  
      return await this._cnnection.info();
    };

    public getOperations:(fileName: string,isDir: boolean)=> Promise<DriverOperationDto[]> = async (fileName: string,isDir: boolean) => {
       
        return await this._cnnection.getOperations(fileName,isDir);
    }
    
    public execute:(driverId:number,operationId:number, path:string,type:string,source?:any)=> Promise<void> = async (driverId:number,operationId:number, path:string,type:string,source?:any) => {
        return await this._cnnection.execute(driverId,operationId,path,type, source);
    }

    public GetDriverList:(driverId?:number)=> Promise<DriverDto[]> = async (driverId?:number) => { 
        return await this._cnnection.GetDriverList(driverId);
    }
 

} 